package com.twu.biblioteca.Models;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Nimrod
 * Date: 2/3/13
 * Time: 2:44 PM
 * Represents a Movie
 */
public class Movie {
    private String name;
    private Date releaseDate;
    private String nameOfDirector;
    private int rating;

    public Movie(String name, Date releaseDate, String nameOfDirector) {
        this.name = name;
        this.releaseDate = releaseDate;
        this.nameOfDirector = nameOfDirector;
        this.rating = MovieRating.NO_RATING;
    }

    //Sets the rating of the movie
    public boolean rate(int rating) {
        if (MovieRating.ratingIsValid(rating)) {
            this.rating = rating;
            return true;
        }
        else
            return false;
    }

    @Override
    @SuppressWarnings("deprecated")
    public String toString() {
        return name + "\t" + releaseDate.getYear() + "\t" + nameOfDirector + "\t" + MovieRating.convertToStarRating(rating);
    }

    private static class MovieRating {

        private static final int MIN_RATING = 1;
        private static final int MAX_RATING = 10;
        public static final int NO_RATING = -1;

        public static boolean ratingIsValid(int rating) {
            return rating >= MIN_RATING && rating <= MAX_RATING;
        }

        public static String convertToStarRating(int rating) {
            String ratingStars = "";
            if (rating == MovieRating.NO_RATING)
                ratingStars = "N/A";
            else {
                for (int r = 1; r <= rating; r++) {
                    ratingStars += "*";
                }
            }
            return ratingStars;
        }
    }
}
